package com.mdms.jsf.managedbeans.dictionaries;

import java.util.ArrayList;
import java.util.List;


import com.mdms.commands.dictionaries.DictionariesCommand;
import com.mdms.dto.MdmDictionaries;
import com.mdms.logger.SimplyMDLogger;

public class DictionariesCache {

	private final static SimplyMDLogger logger = new SimplyMDLogger(DictionariesCache.class);

	private static  List<MdmDictionaries> dictionaries;

	private static boolean isIntiliazed=false;

	public static  MdmDictionaries getDictionaryById(Long id){
		String methodName="getDictionaryById";
		logger.debug(methodName,"START-Getting dictionary from cache id: " +id.toString());
		for (MdmDictionaries mdmDictionaries: dictionaries) {
			if(mdmDictionaries.getDct_id().equals(id)){
				logger.debug(methodName,"END");
				return mdmDictionaries;
			}
		}
		logger.debug(methodName,"END-Dictionary not found id: " +id.toString());
		return null;
	}

	public static MdmDictionaries getDictionaryByTypeAndCode(String type,String code){
		String methodName="getDictionaryByTypeAndCode";
		logger.debug(methodName,"START-Getting dictionary from cache Type and code: " + type + " " + code);
		for (MdmDictionaries mdmDictionaries: dictionaries) {
			if(mdmDictionaries.getDct_type().equals(type) &&
					mdmDictionaries.getDct_code().equals(code)){
				logger.debug(methodName,"END");
				return mdmDictionaries;
			}
		}
		logger.debug(methodName,"END-Dictionary not found Type and code: " + type + " " + code);
		return null;
	}

	public static void initialize() {
		if(!isIntiliazed){
			logger.debug("initialize", "START");
			DictionariesCommand command = new DictionariesCommand();
			try{
				command.setAction(DictionariesCommand.GET_ALL_DICTIONARIES);
				command.execute();
				if(command.getMdmDictList()!=null && !command.getMdmDictList().isEmpty()){
					isIntiliazed=true;
					dictionaries=new ArrayList<MdmDictionaries>();
					dictionaries=command.getMdmDictList();
				}
			}catch(Exception e){
				logger.error("initiazlie", e);
			}
		}
	}

	public static List<MdmDictionaries> getDictionaries() {
		return dictionaries;
	}




}
